<?php
class InspirationsController extends AppController {

	var $name = 'Inspirations';


	function beforeFilter(){
		if ($this->Session->check('inspiration') == false){
			if ($this->params['action']!='add'){
				$this -> redirect('/');
			}
		}

		$restricted_actions = array ('index','edit','delete');
		if (in_array($this->action,$restricted_actions)){
			$this -> redirect('/');
		}
	}
	function index() {
		$this->Inspiration->recursive = 0;
		$this->set('inspirations', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->redirect(array('action' => 'notFound'));
		}
		$_SESSION['Inspiration']['used_ids'][]=$id;
		$this->set('inspiration', $this->Inspiration->read(null, $id));
		$categories = ClassRegistry::init('Category')->find('list');
		$subcategories = $this->Inspiration->Subcategory->find('all');
		$this->set(compact('subcategories'));
		$this->set(compact('categories'));

	}
	function viewRandom($category_id = null) {
		$used_ids=$_SESSION['Inspiration']['used_ids'];
		$sub_id = $this->data['Inspiration']['subcategory_id'];
		if (!$sub_id){
			$sub_id = $_SESSION['Inspiration']['actual_sub_id'][0];
		}
		unset($_SESSION['Inspiration']['actual_sub_id']);
		$_SESSION['Inspiration']['actual_sub_id'][]=$sub_id;
		if(count($used_ids)==1){
			$conditions = array('conditions' => array("Inspiration.id <>" => $used_ids[0],"Inspiration.subcategory_id"=> $sub_id));
		} else{
			$conditions = array('conditions' => array("Inspiration.id not" => $used_ids,"Inspiration.subcategory_id"=> $sub_id));
		}
		$inspirations = $this->Inspiration->find('list',$conditions);
		if ($inspirations){
			$inspiration = array_rand($inspirations,1);
			$this->redirect(array('action' => 'view', $inspirations[$inspiration]));
		} else {
			$this->redirect(array('action' => 'view'));
		}
	}
	function notFound($category_id = null){
		$categories = ClassRegistry::init('Category')->find('list');
		$subcategories = $this->Inspiration->Subcategory->find('all');
		$this->set(compact('subcategories'));
		$this->set(compact('categories'));
	}

	function add() {
		if (!empty($this->data)) {
			unset($_SESSION['Inspiration']['actual_sub_id']);
			$_SESSION['Inspiration']['actual_sub_id'][]=$this->data['Inspiration']['subcategory_id'];
			$this->Inspiration->create();
			$this->data['Inspiration']['date']=null;
			if ($this->Inspiration->save($this->data)) {
				if ($this->Session->check('inspiration.used_ids') == false){
					$this->Session->write('inspiration.used_ids','start');
				}
				$_SESSION['Inspiration']['used_ids'][]=$this->Inspiration->getLastInsertId();
				$this->redirect(array('action' => 'viewRandom'));
			}
		}
		$categories = ClassRegistry::init('Category')->find('list');
		$subcategories = $this->Inspiration->Subcategory->find('all');
		$this->set(compact('subcategories'));
		$this->set(compact('categories'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Inspiration->save($this->data)) {
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The inspiration could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Inspiration->read(null, $id);
		}
		$subcategories = $this->Inspiration->Subcategory->find('list');
		$this->set(compact('subcategories'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Inspiration->delete($id)) {
			$this->Session->setFlash(__('Inspiration deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Inspiration was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
